#include "kdcgeneralconfigpage.h"

#include <kdclabel.h>

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QDir>
#include <QToolButton>

#include <KLocale>
#include <KFileDialog>
#include <KUrl>

KDCGeneralConfigPage::KDCGeneralConfigPage( QWidget * parent ) : KDCConfigPageInterface( parent )
{
	kcfg_DefaultOutputPath = new KLineEdit;
	kcfg_DefaultOutputPath->setReadOnly( TRUE );
	QToolButton *button = new QToolButton;
	button->setText("...");
	kcfg_NiceLevel = new KIntSpinBox;
	kcfg_NiceLevel->setRange( -20, 19 );
	kcfg_BugReport = new QCheckBox( i18n("Show BugReport dialog if nesseserry\n( i.e. when apps crash )" ) );

	QGridLayout *gLayout = new QGridLayout;
	gLayout->addWidget( new QLabel( i18n("Default output path:"), this ), 0, 0 );
	gLayout->addWidget( kcfg_DefaultOutputPath, 0, 1 );
	gLayout->addWidget( button, 0, 2 );
	gLayout->addWidget( new QLabel( i18n("Nice Level:"), this ), 1, 0 );
	gLayout->addWidget( kcfg_NiceLevel, 1, 1 );

	kcfg_UpdateGui = new KComboBox( this );
	kcfg_UpdateGui->addItem( "500 ms", QVariant( 500 ) );
	kcfg_UpdateGui->addItem( "1 s", QVariant( 1000 ) );
	kcfg_UpdateGui->addItem( "1.5 s", QVariant( 1500 ) );
	kcfg_UpdateGui->addItem( "2 s", QVariant( 2000 ) );
	kcfg_UpdateGui->addItem( "3 s", QVariant( 3000 ) );
	kcfg_UpdateGui->addItem( "5 s", QVariant( 5000 ) );
	kcfg_UpdateGui->addItem( "10 s", QVariant( 10000 ) );

	gLayout->addWidget( new QLabel( i18n("GUI update interval:"), this ), 2, 0 );
	gLayout->addWidget( kcfg_UpdateGui, 2, 1 );

	QVBoxLayout *mainLayout = new QVBoxLayout( this );
	mainLayout->addLayout( gLayout );
	mainLayout->addWidget( kcfg_BugReport );
	mainLayout->addStretch();

	connect( button, SIGNAL( clicked() ), this, SLOT( changeOutputPatch() ) );
}

KDCGeneralConfigPage::~KDCGeneralConfigPage()
{

}

void KDCGeneralConfigPage::load()
{
	kcfg_DefaultOutputPath->setText( KDCConfig::defaultOutputPath() );
	kcfg_NiceLevel->setValue( KDCConfig::niceLevel() );
	kcfg_UpdateGui->setCurrentIndex( kcfg_UpdateGui->findData( KDCConfig::updateGui() ) );
	kcfg_BugReport->setChecked( KDCConfig::bugReport() );
}

void KDCGeneralConfigPage::save()
{
	KDCConfig::setDefaultOutputPath( kcfg_DefaultOutputPath->text() );
	KDCConfig::setNiceLevel( kcfg_NiceLevel->value() );
	KDCConfig::setUpdateGui( kcfg_UpdateGui->itemData( kcfg_UpdateGui->currentIndex() ).toInt() );
	KDCConfig::setBugReport( kcfg_BugReport->checkState() == Qt::Checked );
}

void KDCGeneralConfigPage::changeOutputPatch()
{
	KUrl path = KFileDialog::getExistingDirectoryUrl( KUrl( kcfg_DefaultOutputPath->text() ), this, i18n("Select Default Output Directory") );
	if ( !path.isEmpty() )
		kcfg_DefaultOutputPath->setText( path.toLocalFile() );
}
